package com.myfoon.model.entryprocessor;

import com.tangosol.util.InvocableMap;
import com.tangosol.util.processor.AbstractProcessor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class EventFinder extends AbstractProcessor {
    private int start;
    private int max;
    
    public EventFinder() {
    }
    
    public EventFinder(int start, int max) {
        this.start = start;
        this.max = max;
    }

    public Object process(InvocableMap.Entry entry) {        
        if (entry == null) 
            return null;
        
        List<Long> inbox;
        if (entry.getValue() == null) {                        
            inbox = new ArrayList<Long>();
            entry.setValue(inbox);
            return inbox;
        }
                
        inbox = (List<Long>)entry.getValue();        
                        
        if (inbox.size() == 0) {
            return inbox;
        }
        
        List<Long> inboxSublist = new ArrayList<Long>();
        Collections.reverse(inbox);
                
        if (start < inbox.size() ) {
            if ( start + max < inbox.size() ) {
                inboxSublist.addAll(inbox.subList(start, start + max));                
            }
            else {
                inboxSublist.addAll(inbox.subList(start, inbox.size()));                
            }
        }        
        
        return inboxSublist;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getStart() {
        return start;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public int getMax() {
        return max;
    }
}
